package com.dman.multi.core;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;


public class DownloadSettings implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	private int maxConcurrentDownloads;
	private String connectionSpeed;
	private long connSpeed;
	private ProxySettings proxySettings;
	private Map<String, Long> unitMultiplicationFactor = null;
	
	public static String SETTINGS_FILE_NAME = Download.FULL_CONF_DIR + File.separator + "javadm.settings";
	
	public DownloadSettings(int maxConcurrentDownloads, String connectionSpeed, ProxySettings proxySettings) {
		this.maxConcurrentDownloads = maxConcurrentDownloads;
		this.connectionSpeed = connectionSpeed;
		this.proxySettings = proxySettings;
		unitMultiplicationFactor = new HashMap<String, Long>();
		unitMultiplicationFactor.put("bps", 1l);
		unitMultiplicationFactor.put("kbps", 1l<<10);
		unitMultiplicationFactor.put("mbps", 1l<<20);
		unitMultiplicationFactor.put("gbps", 1l<<30);
		unitMultiplicationFactor.put("tbps", 1l<<40);
		String [] sarr = connectionSpeed.split(" ");
		connSpeed = Integer.parseInt(sarr[0]) * unitMultiplicationFactor.get(sarr[1]);
	}
	
	public void setMaxConcurrentDownloads(int maxConcurrentDownloads) {
		this.maxConcurrentDownloads = maxConcurrentDownloads;
	}

	public void setConnectionSpeed(String connectionSpeed) {
		this.connectionSpeed = connectionSpeed;
		String [] sarr = connectionSpeed.split(" ");
		connSpeed = Integer.parseInt(sarr[0]) * unitMultiplicationFactor.get(sarr[1]);
	}

	public void setProxySettings(ProxySettings proxySettings) {
		this.proxySettings = proxySettings;
	}

	public int getMaxConcurrentDownloads() {
		return maxConcurrentDownloads;
	}
	
	public String getConnectionSpeed() {
		return connectionSpeed;
	}
	
	public ProxySettings getProxySettings() {
		return proxySettings;
	}
	
	public long getConnSpeed(){
		return connSpeed;
	}
	
	public void save() throws IOException{
		File f = new File(SETTINGS_FILE_NAME);
		f.createNewFile();
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
		oos.writeObject(this);
	}
}
